import {filterData2,getUrlParam,age} from '@/static/js/filter.js'
export default {
	data() {
		const currentDate = this.getDate({
			format: true
		})
		const _this = this;
		return {
			checkbox: false,
			show: false,
			speciShow: false,
			dialog:false,
			timeShow: false,
			nation: '',
			bloodGroup: '',
			educationLevel: '',
			occupation: '',
			maritalStatus: '',
			medicalExpensesType: '',
			occupation: '',
			emergencyContactName: '',
			emergencyContactPhone: '',
			employer: '',
			genetic: '', //遗传病史
			defaultArr: [], //默认下拉框数组
			confirmButton: '',  //下拉框确认按钮
			showModal:false,
			showContent:'',
			national: [ //民族
				{value:1 ,label:'汉族'},
				{value:2 ,label:'蒙古族'},
				{value:3 ,label:'回族'},
				{value:4 ,label:'藏族'},
				{value:5 ,label:'维吾尔族'},
				{value:6 ,label:'苗族'},
				{value:7 ,label:'彝族'},
				{value:8 ,label:'壮族'},
				{value:9 ,label:'布依族'},
				{value:10,label:'朝鲜族'},
				{value:11,label:'满族'},
				{value:12,label:'侗族'},
				{value:13,label:'瑶族'},
				{value:14,label:'白族'},
				{value:15,label:'土家族'},
				{value:16,label:'哈尼族'},
				{value:17,label:'哈萨克族'},
				{value:18,label:'傣族'},
				{value:19,label:'黎族'},
				{value:20,label:'傈僳族'},
				{value:21,label:'佤族'},
				{value:22,label:'畲族'},
				{value:23,label:'高山族'},
				{value:24,label:'拉祜族'},
				{value:25,label:'水族'},
				{value:26,label:'东乡族'},
				{value:27,label:'纳西族'},
				{value:28,label:'景颇族'},
				{value:29,label:'柯尔克孜族'},
				{value:30,label:'土族'},
				{value:31,label:'达翰尔族'},
				{value:32,label:'么佬族'},
				{value:33,label:'羌族'},
				{value:34,label:'布朗族'},
				{value:35,label:'撒拉族'},
				{value:36,label:'毛南族'},
				{value:37,label:'仡佬族'},
				{value:38,label:'锡伯族'},
				{value:39,label:'阿昌族'},
				{value:40,label:'普米族'},
				{value:41,label:'塔吉克族'},
				{value:43,label:'乌孜别克族'},
				{value:44,label:'俄罗斯族'},
				{value:45,label:'鄂温克族'},
				{value:46,label:'德昂族'},
				{value:47,label:'保安族'},
				{value:48,label:'裕固族'},
				{value:49,label:'京族'},
				{value:50,label:'塔塔尔族'},
				{value:51,label:'独龙族'},
				{value:52,label:'鄂伦春族'},
				{value:53,label:'赫哲族'},
				{value:54,label:'门巴族'},
				{value:55,label:'珞巴族'},
				{value:56,label:'基诺族'},
			],
			bloodType: [ //血型
				{value:1,label:'A型'},
				{value:2,label:'B型'},
				{value:3,label:'O型'},
				{value:4,label:'AB型'},
				{value:5,label:'RH阴性'},
				{value:6,label:'RH阳性'},
				{value:7,label:'不详'},
			],
			levelEducation: [ //文化程度
				{value:1,label:'研究生'},
				{value:2,label:'大学本科'},
				{value:3,label:'大学专科和专科学校'},
				{value:4,label:'中等专业学校'},
				{value:5,label:'技工学校'},
				{value:6,label:'高中'},
				{value:7,label:'初中'},
				{value:8,label:'小学'},
				{value:9,label:'文盲或半文盲'},
				{value:10,label:'不详'},
			],
			professional: [ //职业
				{value:1,label:'国家机关、党群组织、企业、事业单位负责人'},
				{value:2,label:'专业技术人员'},
				{value:3,label:'办事人员和有关人员'},
				{value:4,label:'商业、服务业人员'},
				{value:5,label:'农、林、牧、渔、水利业生产人员'},
				{value:6,label:'生产、运输设备操作人员及有关人员'},
				{value:7,label:'军人'},
				{value:8,label:'不便分类的其他从业人员'},
				{value:9,label:'无职业'}
			],
			maritalType: [//婚姻状况
				{value:1,label:'未婚'},
				{value:2,label:'已婚'},
				{value:3,label:'丧偶'},
				{value:4,label:'离婚'},
				{value:5,label:'未说明的婚姻状况'}
			],
			medicalPayments: [ //医疗支付
				{value:1,label:'城镇职工基本医疗保险'},
				{value:2,label:'城镇居民基本医疗保险'},
				{value:3,label:'新型农村合作医疗'},
				{value:4,label:'贫困救助'},
				{value:5,label:'商业医疗保险'},
				{value:6,label:'全公费'},
				{value:7,label:'全自费'},
				{value:8,label:'其他'},
			], 
			historyName: [ //既往史名称
				{value:2,label:'高血压'},
				{value:3,label:'糖尿病'},
				{value:4,label:'冠心病'},
				{value:5,label:'慢性阻塞性肺疾病'},
				{value:6,label:'恶性肿瘤'},
				{value:7,label:'脑卒中'},
				{value:8,label:'严重精神障碍'},
				{value:9,label:'结核病'},
				{value:10,label:'肝炎'},
				{value:11,label:'其他法定传染病'},
				{value:12,label:'职业病'},
				{value:13,label:'其他'},
			],
			historyArr: [ //药物过敏史
				{name: '青霉素',checked: false},
				{name: '磺胺',checked: false},
				{name: '链霉素',checked: false},
				{name: '其他',checked: false},
			],
			exposureHistory: [ //暴露史
				{name: '化学品',checked: false},
				{name: '毒物',checked: false},
				{name: '射线',checked: false}
			],
			fatherArr: [
				{value:false,name:'高血压'},
				{value:false,name:'糖尿病'},
				{value:false,name:'冠心病'},
				{value:false,name:'慢性阻塞性肺疾病'},
				{value:false,name:'恶性肿瘤'},
				{value:false,name:'脑卒中'},
				{value:false,name:'严重精神障碍'},
				{value:false,name:'结核病'},
				{value:false,name:'肝炎'},
				{value:false,name:'先天畸形'},
				{value:false,name:'其他'},
			],
			motherArr: [
				{value:false,name:'高血压'},
				{value:false,name:'糖尿病'},
				{value:false,name:'冠心病'},
				{value:false,name:'慢性阻塞性肺疾病'},
				{value:false,name:'恶性肿瘤'},
				{value:false,name:'脑卒中'},
				{value:false,name:'严重精神障碍'},
				{value:false,name:'结核病'},
				{value:false,name:'肝炎'},
				{value:false,name:'先天畸形'},
				{value:false,name:'其他'},
			],
			sisterArr: [
				{value:false,name:'高血压'},
				{value:false,name:'糖尿病'},
				{value:false,name:'冠心病'},
				{value:false,name:'慢性阻塞性肺疾病'},
				{value:false,name:'恶性肿瘤'},
				{value:false,name:'脑卒中'},
				{value:false,name:'严重精神障碍'},
				{value:false,name:'结核病'},
				{value:false,name:'肝炎'},
				{value:false,name:'先天畸形'},
				{value:false,name:'其他'},
			],
			sonArr: [
				{value:false,name:'高血压'},
				{value:false,name:'糖尿病'},
				{value:false,name:'冠心病'},
				{value:false,name:'慢性阻塞性肺疾病'},
				{value:false,name:'恶性肿瘤'},
				{value:false,name:'脑卒中'},
				{value:false,name:'严重精神障碍'},
				{value:false,name:'结核病'},
				{value:false,name:'肝炎'},
				{value:false,name:'先天畸形'},
				{value:false,name:'其他'},
			],
			disabilityArr: [
				{value:2,name:'视力残疾'},
				{value:3,name:'听力残疾'},
				{value:4,name:'言语残疾'},
				{value:5,name:'肢体残疾'},
				{value:6,name:'智力残疾'},
				{value:7,name:'精神残疾'},
				{value:8,name:'其他残疾'}
			],
			sexArr: [
				{value:1,label:'男'},
				{value:2,label:'女'},
			],
			confirmTimeButton: '',
			userName:'' ,//会员名字
			userId: '', //会员id
			historytitle:'', //药物过敏史
			exposureHistorytitle: '', //暴露史
			fathertitle: '', //父亲
			monthertitle: '', //母亲
			sistertitle: '', //兄弟姐妹
			sontitle: '', //子女
			disabilitytitle: '', //残疾情况
			memberInformation: {}, //会员详情
			pastHistorydisease: [] , //既往史（疾病）
			pastHistorysurgery: [], //既往史 （手术）
			pastHistoryTraumae: [], //既往史（外伤）
			pastHistorytransfusion: [] ,//既往史（输血）
			geneticDisease: [], //遗传病史
			id: '',
			userInforDetail: {} ,//详情
			userinfor:{}, //缓存中的用户信息
			date: currentDate,
			endDate:currentDate,
			sex:'',
			ageNum:'',
			start_date: '', //传选中的填写日期给后台
			selected:''
		}
	},
	computed:{
		endYear(){
			return this.filterData2(new Date().getTime()).substr(0,4)
		},
		startDate() {
			return this.getDate('start');
		}
	},
	created() {
		this.findUser();
		const id = getUrlParam('id');
		if(id) {
			this.id = id;
			this.$api.UserInfoDetails({id:id}).then((res) => {
				if(res.data.returnCode == '200') {
					// this.userInforDetail = res.data.result;
					this.memberInformation = res.data && res.data.result;
					this.nation = res.data.result && res.data.result.nation;
					this.employer = res.data.result && res.data.result.employer;
					this.emergencyContactName = res.data.result && res.data.result.emergencyContactName;
					this.emergencyContactPhone = res.data.result && res.data.result.emergencyContactPhone;
					this.bloodGroup = res.data.result && res.data.result.bloodGroup;
					this.educationLevel = res.data.result &&  res.data.result.educationLevel;
					this.occupation = res.data.result && res.data.result.occupation;
					this.maritalStatus = res.data.result && res.data.result.maritalStatus;
					this.medicalExpensesType = res.data.result && res.data.result.medicalExpensesType;
					this.sex = res.data.result && res.data.result.sex == 2 ? '女' : '男';
					this.ageNum = res.data.result && res.data.result.age;
					// drugAllergy
					this.historytitle = '药物过敏史：'+ (res.data && res.data.result.drugAllergy || '无')
					this.historyArr = this.historyArr.map((a)=>{
						if(res.data.result.drugAllergy && res.data.result.drugAllergy.indexOf(a.name) > -1){
							a.checked = true
						}
						return a;
					})
					this.exposureHistorytitle = '暴露史：'+ (res.data.result && res.data.result.exposureHistory || '无');
					this.exposureHistory = this.exposureHistory.map((a) => {
						if(res.data.result.exposureHistory && res.data.result.exposureHistory.indexOf(a.name) > -1){
							a.checked = true
						}
						return a;
					})
					this.pastHistorydisease = res.data.result.pastHistory[0]&& res.data.result.pastHistory[0].parameterValueRes
					this.pastHistorysurgery = res.data.result.pastHistory[1]&& res.data.result.pastHistory[1].parameterValueRes
					this.pastHistoryTraumae = res.data.result.pastHistory[2].parameterValueRes
					this.pastHistorytransfusion = res.data.result.pastHistory[3].parameterValueRes
					var fathertitle = ''
					this.fatherArr = this.fatherArr.map((a) => {
						res.data.result.familyHistory[0].parameterValueRes.map((b)=>{
							if(a.name == b.value){
								a.value = true
								fathertitle += b.value+','
							}
						})
						return a;
					})
					this.fathertitle = '父亲：'+ fathertitle.substr(0,fathertitle.length-1)
					
					var monthertitle = ''
					this.motherArr = this.motherArr.map((a) => {
						res.data.result.familyHistory[1].parameterValueRes.map((b)=>{
							if(a.name == b.value){
								a.value = true
								monthertitle += b.value+','
							}
						})
						return a;
					})
					this.monthertitle = '母亲：'+ monthertitle.substr(0,monthertitle.length-1)
					
					var sistertitle = ''
					this.sisterArr = this.sisterArr.map((a) => {
						res.data.result.familyHistory[2].parameterValueRes.map((b)=>{
							if(a.name == b.value){
								a.value = true
								sistertitle += b.value+','
							}
						})
						return a;
					})
					this.sistertitle = '兄弟姐妹：'+ sistertitle.substr(0,sistertitle.length-1)
					
					var sontitle = ''
					this.sonArr = this.sonArr.map((a) => {
						res.data.result.familyHistory[3].parameterValueRes.map((b)=>{
							if(a.name == b.value){
								a.value = true
								sontitle += b.value+','
							}
						})
						return a;
					})
					this.sontitle = '子女：'+ sontitle.substr(0,sontitle.length-1)
					this.geneticDisease = res.data.result.geneticDisease && res.data.result.geneticDisease.split(',').map((a)=>{return {genetic:a}}) || [];
					
					this.disabilitytitle = '残疾情况：'+(res.data.result.disability || '无');
					this.disabilityArr = this.disabilityArr.map((a) => {
						if(res.data.result.disability && res.data.result.disability.indexOf(a.name) > -1){
							a.checked = true
						}
						return a;
					}) || []
					// console.log(this.userInforDetail)
				}
			})
		}
		
		this.userinfor = JSON.parse(localStorage.getItem('userInfo'));

	},
	methods: {
		filterData2,
		age,
		// 日期
		bindDateChange(e) {
			this.start_date = e.target.value;
		},
		getDate(type) {
			const date = new Date();			
			let year = date.getFullYear();		
			let month = date.getMonth() + 1;			
			let day = date.getDate();		
			if (type === 'start') {
				year = year - 60;		
			} else if (type === 'end') {
				year = year + 2;
			
			}	
			month = month > 9 ? month : '0' + month;;			
			day = day > 9 ? day : '0' + day;			
			return `${year}-${month}-${day}`;		
		},
		// 选择确认按钮
		confirm(e) {
			this.confirmButton == 'national' ? this.nation = e[0].label : 
			this.confirmButton == 'bloodType' ? this.bloodGroup = e[0].label : 
			this.confirmButton == 'levelEducation' ? this.educationLevel = e[0].label : 
			this.confirmButton == 'professional' ? this.occupation = e[0].label : 
			this.confirmButton == 'maritalType' ? this.maritalStatus = e[0].label : 
			this.confirmButton == 'medicalPayments' ? this.medicalExpensesType = e[0].label : 
			this.confirmButton == 'historyName' ? this.popData[this.popindex].name = e[0].label : 
			this.confirmButton == 'historyNameTwo' ? this.popData[this.popindex].name = e[0].label : 
			this.confirmButton == 'historyNamethree' ? this.popData[this.popindex].name = e[0].label :
			this.confirmButton == 'historyNameFour' ? this.popData[this.popindex].name = e[0].label :
			this.confirmButton == 'sex' ? this.sex = e[0].label : ''
		},
		// 选择弹出
		popUp(e,index,data) {
			this.show = true;
			this.confirmButton = e;
			this.popindex = index;
			this.popData = data
			this.defaultArr = e == 'national' ? this.national :  //民族
							  e == 'bloodType' ? this.bloodType :   //血型
							  e == 'levelEducation' ? this.levelEducation :  //文化程度
							  e == 'professional' ? this.professional :  //职业
							  e == 'maritalType' ? this.maritalType :  //婚姻状况
							  e == 'medicalPayments' ? this.medicalPayments : //医疗费用支付
							  e == 'historyName' ? this.historyName : 
							  e == 'historyNameTwo' ? this.historyName : 
							  e == 'historyNamethree' ? this.historyName : 
							  e == 'historyNameFour' ? this.historyName : 
							  e == 'sex' ? this.sexArr : []
		},
		// 时间弹框
		timePopUp(e,index,data) {	
			this.timeShow = true
			this.confirmTimeButton = e;
			this.popindex = index;
			this.popData = data;
		},
		timeConfirm(e) { //时间确认弹框
			this.popData[this.popindex].value = e.detail.value
			// this.confirmTimeButton == 'historyName' ? this.popData[this.popindex].value = e.year +'-'+ e.month +'-'+ e.day : 
			// this.confirmTimeButton == 'historyNameTwo' ? this.popData[this.popindex].value = e.year +'-'+ e.month +'-'+ e.day :
			// this.confirmTimeButton == 'historyNameThree' ? this.popData[this.popindex].value = e.year +'-'+ e.month +'-'+ e.day :''
			// this.confirmTimeButton == 'historyNameFour' ? this.popData[this.popindex].value = e.year +'-'+ e.month +'-'+ e.day :''
		},
		// 多选框
		checkboxChange(e,i,arr,name) {
			if(arr[i].checked){
				arr[i].checked = false
			}else{
				arr[i].checked = true
			}
			const isCheck = arr.filter((a)=>{ return a.checked })
			var isCheckResult = isCheck.map((a)=>{return a.name}).join(',')
			if(name == 'history'){
				this.historytitle = '药物过敏史：'+isCheckResult;
			} else if(name == 'exposureHistoryArr') {
				this.exposureHistorytitle = '暴露史：'+isCheckResult
			} else if (name == 'fatherArray') {
				this.fathertitle = '父亲：' + isCheckResult
			}else if (name == 'montherArray') {
				this.monthertitle = '母亲：' + isCheckResult
			}else if (name == 'sisterArray') {
				this.sistertitle = '兄弟姐妹：' + isCheckResult
			}else if (name == 'sonArray') {
				this.sontitle = '子女：' + isCheckResult
			}else if(name == 'disabilityArray') {
				this.disabilitytitle = '残疾情况：' + isCheckResult
			}

		},
		// 根据选择会员的id查会员信息
		findUser(){
			this.$api.findById().then((res) => {
				if(res.data.returnCode == '200') {
					this.memberInformation = res.data.result;
				}
			})

		},
		historyNameOne() { //点击添加既往史(疾病)
			this.pastHistorydisease.push({name:'',value:''});
		},
		historyNameTwo() { //点击添加既往史(手术)
			this.pastHistorysurgery.push({name:'',value:''});
		},
		historyNameThree() {//点击添加既往史(外伤)
			this.pastHistoryTraumae.push({name:'',value:''});
		},
		historyNameFour() {//点击添加既往史(输血)
			this.pastHistorytransfusion.push({name:'',value:''});
		},
		geneticHistoryAdd() { //添加遗传病史
			this.geneticDisease.push({})
		},
		historyNameOneDel(index) { //点击删除既往史（疾病）
			this.pastHistorydisease.splice(index,1);
		},
		historyNametwoDel(index) {//点击删除既往史（手术）
			this.pastHistorysurgery.splice(index,1)
		},
		historyNamethreeDel(index) {//点击删除既往史（手术）
			this.pastHistoryTraumae.splice(index,1)
		},
		historyNamefourDel(index) {//点击删除既往史(输血)
			this.pastHistorytransfusion.splice(index,1)
		},
		geneticHistoryDel(index) {
			this.geneticDisease.splice(index,1)
		},
		save (e) { //保存方法
			if(!this.sex){
				uni.showToast({title:'请选择性别',icon:'none'})
				return;
			}
			if(!this.ageNum){
				uni.showToast({title:'请填写年龄',icon:'none'})
				return;
			}
			const pastHistory = [
				{
					name:'既往史（疾病）',
					parameterValueRes: this.pastHistorydisease.length <= 0 ? [] : this.pastHistorydisease
				},
				{
					name:'既往史（手术）',
					parameterValueRes: this.pastHistorysurgery.length <= 0 ? [] : this.pastHistorysurgery
				},
				{
					name:'既往史（外伤）',
					parameterValueRes: this.pastHistoryTraumae.length <= 0 ? [] : this.pastHistoryTraumae
				},
				{
					name:'既往史（输血）',
					parameterValueRes: this.pastHistorytransfusion.length <= 0 ? [] : this.pastHistorytransfusion
				}
			]
			var fathertitleArr = this.fathertitle.substr(3).split(",");
			var monthertitleArr = this.monthertitle.substr(3).split(",");
			var sistertitleArr = this.sistertitle.substr(5).split(",");
			var sontitleArr = this.sontitle.substr(3).split(",");
			const familyHistory = [			
				{
					name:'父亲',
					parameterValueRes: fathertitleArr.length > 0 && fathertitleArr[0] != '' && fathertitleArr.map((a,i) => {
						return {
							name:i,
							value: a
						}
					}) || []
				},
				{
					name:'母亲',
					parameterValueRes:monthertitleArr.length > 0 && monthertitleArr[0] != '' && monthertitleArr.map((a,i) => {
						return {
							name:i,
							value: a
						}
					}) || []
				},
				{
					name:'兄弟姐妹',
					parameterValueRes: sistertitleArr.length > 0 && sistertitleArr[0] != '' &&  sistertitleArr.map((a,i) => {
						return {
							name:i,
							value: a
						}
					}) || []
				},
				{
					name:'子女',
					parameterValueRes: sontitleArr.length > 0 && sontitleArr[0] != '' && sontitleArr.map((a,i) => {
						return {
							name:i,
							value: a
						}
					}) || []
				}
				
			]
			const geneticDisease = this.geneticDisease.map((a) => {
				return a.genetic
			})
			const AppUserInfoRes = {
				userId: this.memberInformation.userId,
				age:this.ageNum,
				realName:this.memberInformation.realName,
				idCard:this.memberInformation.idCard,
				sex: this.sex == '男'?1:2,
				birthday: this.memberInformation.birthday,
				bloodGroup: this.bloodGroup,
				phone: this.memberInformation.phone,
				nation: this.nation,
				employer: this.employer,
				emergencyContactName: this.emergencyContactName,
				emergencyContactPhone: this.emergencyContactPhone,
				educationLevel: this.educationLevel,
				occupation: this.occupation,
				maritalStatus: this.maritalStatus,
				medicalExpensesType: this.medicalExpensesType,
				drugAllergy: this.historytitle.substr(6) || '',
				exposureHistory: this.exposureHistorytitle.substr(4) || '' ,
				pastHistory: pastHistory,
				familyHistory: familyHistory,
				geneticDisease: geneticDisease.toString(),
				disability: this.disabilitytitle.substr(5),
				sources: 1,
				status: e,
			}

			if(this.id) {
				AppUserInfoRes.id = this.id
				if(e == 1) {
					this.$api.userinforUpdate(AppUserInfoRes).then((res) => {
						if(res.data.returnCode =='200') {
							uni.showToast({
								title: '修改成功',
								icon: 'none'						
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/monitor/information/list'
								})
							},1000)
						}
					})
				} else {
					this.qianIdSave(AppUserInfoRes)
				}
				
			}else{
				AppUserInfoRes.healthId = this.userinfor && this.userinfor.healthId || ''
				// 新增
				if(e == 1) {
					this.defaultSave(AppUserInfoRes)
				} else {
					this.qianSave(AppUserInfoRes)
				}
			}
		},
		// 普通保存
		defaultSave(AppUserInfoRes){
			this.$api.userInfoAdd(AppUserInfoRes).then((res) => {
				if(res.data.returnCode =='200') {
					uni.showToast({title: '保存成功',icon: 'none'})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/monitor/information/list'
						})
					},1000)
				}else{
					uni.showToast({title: res.data.message ,icon: 'none'})
				}
			})
		},
		// 签名保存
		qianSave(AppUserInfoRes){
			this.$api.userInfoAdd(AppUserInfoRes).then((res) => {
				if(res.data.returnCode =='200') {
					this.showModal = false;
					uni.showToast({
						title: '存档成功',
						icon: 'none'						
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/monitor/information/list'
						})
					},1000)
				}
			})
		},
		// 有id的签名发送
		qianIdSave(AppUserInfoRes){
			this.$api.userinforUpdate(AppUserInfoRes).then((res) => {
				if(res.data.returnCode =='200') {
					this.showModal = false;
					uni.showToast({
						title: '存档成功',
						icon: 'none'						
					})
					setTimeout(() => {
						uni.navigateTo({
							url:'/pages/monitor/information/list'
						})
					},1000)
				}
			})
		},
		showModalBtn(e){
			if(e == 2){
				this.showModal = true
				this.showContent = '确认信息无误,存档后不可修改';
				return
			}
			this.save(1)
		},
		sureconfirm() {
			this.save(2)
		},
		cancel(){
			this.showModal = false;
		},
	}
}